﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Input;
using AIPASoft.Interfaces.Base;
using AIPASoft.Interfaces.BusinessDomain;
using AIPASoft.Interfaces.Security;

namespace AIPASoft.SecurityLibrary.ViewModel
{
    class MenuItemsViewModel : ViewModelBase, IDisposable
    {
        private IApplicationManager _applicationManager;

        public MenuItemsViewModel(IApplicationManager applicationManager)
        { 
            InitializeMenuItems();
            _applicationManager = applicationManager;
        }

        private void InitializeMenuItems()
        {
            m_RibbonMenuItems = new List<RibbonApplicationMenuItem>();
            LoginMenuHeader = "Login";
            var loginHeaderBinding = new Binding {Source = this, Path = new PropertyPath("LoginMenuHeader")};

            var mnuItem = new RibbonApplicationMenuItem { Header=LoginMenuHeader, Command = ShowLoginCommand };
            //mnuItem.SetBinding(HeaderedContentControl.HeaderProperty, loginHeaderBinding);


            m_RibbonMenuItems.Add(mnuItem);

            mnuItem = new RibbonApplicationMenuItem { Header = "Exit", Command = ApplicationExitCommand };
            m_RibbonMenuItems.Add(mnuItem);
        }

        private string LoginMenuHeader { get; set; }

        private IUserRealm _userRealm;
        public IUserRealm UserRelam
        {

            get { return _userRealm; }
            set
            {
                _userRealm = value;
                NotifyPropertyChanged("UserRealm");
            }
        }

        private List<RibbonApplicationMenuItem> _mRibbonMenuItems;
        public List<RibbonApplicationMenuItem> m_RibbonMenuItems
        {
            get { return _mRibbonMenuItems; }
            private set
            {
                _mRibbonMenuItems = value;
                NotifyPropertyChanged("m_RibbonMenuItems");
            }
        }

        private ICommand _showLoginCommand;

        private ICommand ShowLoginCommand
        {
            get
            {
                return _showLoginCommand ?? (_showLoginCommand = new RelayCommand(param => this.ShowLogin(), null
                                                                     ));
            }
        }


        private ICommand _showSecurityConsoleCommand;
        public ICommand ShowSecurityConsoleCommand
        {
           get
           {
               return _showSecurityConsoleCommand ??
                      (_showSecurityConsoleCommand = new RelayCommand(param => this.ShowSecurityConsole(), null));
           }
        }


        private ICommand _applicationExitCommand;

        private ICommand ApplicationExitCommand
        {
            get
            {
                return _applicationExitCommand ?? (_applicationExitCommand = new RelayCommand(param => this.ApplicationExit(), null
                                                                     ));
            }
        }


        

        private void ShowSecurityConsole()
        {

        }

        private void ApplicationExit()
        {

            var res = MessageBox.Show("Do you want to exit ?", "", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (res == MessageBoxResult.Yes)
            Application.Current.Shutdown(0);
        }


        private LoginWindow _loginWindow;
        LoginWindowViewModel _loginWindowViewModel = new LoginWindowViewModel();

        private void ShowLogin()
        {
            if (LoginMenuHeader == "Logoff")
                this.UserRelam = null;

            _loginWindow = new LoginWindow()
            {
                WindowStyle = WindowStyle.SingleBorderWindow,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                Title = "Login",
                DataContext = _loginWindowViewModel
            };
             
            _loginWindowViewModel.OnWindowClose += CloseLoginDialog; 
            _loginWindow.Width = 320; 
            _loginWindow.Height = 280; 
            _loginWindow.ShowDialog();
            if (_loginWindowViewModel.UserRelam != null)
                LoginMenuHeader = "Logoff";
        }
  
        private void CloseLoginDialog(object source, EventArgs e)
        {
            _loginWindow.Close();
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
